<?php
/**
 * 百度地图API封装类
 */
class MapApi
{
    const KEY = '808a4b0109b9b5df2208be3056bab925';
    const MAP_HOST = 'http://api.map.baidu.com/';
    
    
	/**
     * 
     * 地址解析
     * @param string $location 经纬度
     * @param string $output
     */
    public static function geocoderByLocation($location, $output = 'json')
    {
        $url = self::MAP_HOST."geocoder?location={$location}&output={$output}&key=".self::KEY;
        $return = Http::curlRequest($url);
        $info = $return ? json_decode($return, true) :  false;
        $address = null;
        if(is_array($info) && $info['status'] == 'OK')
        {
            $address = $info['result']['formatted_address'];
        }
        return $address;
    }
    
    /**
     * 
     * 反向地址解析（地址查询）
     * @param string $address
     * @param string $output
     * @return array
     */
    public static function geocoderByAddress($address, $output = 'json')
    {
        $url = self::MAP_HOST."geocoder?address={$address}&output={$output}&key=".self::KEY;
        $return = Http::curlRequest($url);
        $info = $return ? json_decode($return, true) :  false;
        $data = array();
        if(is_array($info) && $info['status'] == 'OK') {
            $data['lng'] = $info['result']['location']['lng'];
            $data['lat'] = $info['result']['location']['lat'];
        }
		if(!$data['lng'] OR !$data['lat']) return false;
        else return $data;
    }
    
    /*
     * 此服务暂未开放
     *  
    public static function direction($location, $output = 'json')
    {
        $url = self::MAP_HOST."direction?origin=广州天河北路&destination=上社&region=广州&referer=mao&output={$output}";
        echo $url;
        $return = Http::curlRequest($url);
        echo $return;
        $info = $return ? json_decode($return, true) :  false;
        $data = array();
        dump($info);
        if(is_array($info) && $info['status'] == 'OK')
        {
            
        }
        return $data;
    }*/
   
    
}